package sg.edu.nus.iss.phoenix.core.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sg.edu.nus.iss.phoenix.authenticate.entity.User;
 
/**
 * Servlet implementation class PhoenixFrontController
 *
 * @author Swaminathan Subramanian (A0076756)
 * @version 1.0 2012/9/26
 */ 
public class PhoenixFrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public PhoenixFrontController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// System.out.println("sdsdsd");
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		processRequest(request, response);
	}

	/**
	 * Process requests from clients.
	 */
	protected void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// System.out.println(request.getPathInfo());
		String pathInfo = request.getPathInfo();
		String action = FCUtilities.stripPath(pathInfo);
		// System.out.println("PATH" + pathInfo);
		// System.out.println("ACTION" + action);

		String result = null;
		User user = (User) request.getSession().getAttribute("user");

	    result = chooseUseCase(action, user);
		

		// System.out.println("RESULT :" + result);

		RequestDispatcher rd = getServletContext().getRequestDispatcher(result);
		rd.forward(request, response);
	}

	private String chooseUseCase(String action, User user) {
		String ss = "";
		switch (action) {
		case "login":
			return "/LoginController/login";
		case "searchrp":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ProcessController/search";
		case "setuprp":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ProcessController/process";
		case "crudrp":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/CRUDRpController";
		case "loadrp":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ProcessController/load";
		case "deleterp":
			return "/ProcessController/delete";
		case "addschedule":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ScheduleController/create";
		case "loadschedule":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ScheduleController/load";
		case "editschedule":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ScheduleController/edit";
		case "setupschedule":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ScheduleController/process";
		case "deleteschedule":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ScheduleController/delete";
		case "selectschedule":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ScheduleController/select";
		case "copyschedule":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/ScheduleController/copy";
		case "logout":
			return "/LoginController/logout";
		case "loaduser":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/UserController/load";
		case "deleteuser":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/UserController/delete";
		case "adduser":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/UserController/add";
		case "edituser":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/UserController/edit";
		case "setupuser":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/UserController/process";
		case "selectuser":
			ss = redirectUser(user);
			if(ss.length() > 0){
				return ss;
			}
			return "/UserController/select";
		default:
			return "/welcome.jsp";
		}
	}
	
	private String redirectUser(User user){
		if(user == null){
			return "/pages/login.jsp";
		}
		return "";
	}

}
